package com.vanpos.dao.user;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.vanpos.domain.user.OperatorUser;

/**
 * 后台登录用户DAO
 * @author Administrator
 */
@Repository
public class OperatorUserDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 查询登录名、密码是否匹配
	 * @param loginName
	 * @param password
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public int getMatchCount(String loginName, String password){
		String sql = "select count(*) from info_operator_user "
			+ " where loginName=? and password=? and deleted=0";
		return jdbcTemplate.queryForInt(sql, new Object[]{loginName,password});
	}
	
	/**
	 * 通过登录名查询后台用户信息
	 * @param loginName
	 * @return
	 */
	public OperatorUser findUserByLoginName(final String loginName){
		String sql = "select id,roleId,organizationId,loginName,userName,email,rPOrganizationId from info_operator_user "
			+ " where loginName=? and deleted=0";
		final OperatorUser operatorUser = new OperatorUser();
		jdbcTemplate.query(sql, new Object[]{loginName}, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException{
				operatorUser.setId(rs.getLong("id"));
				operatorUser.setRoleId(rs.getLong("roleId"));
				operatorUser.setOrganizationId(rs.getLong("organizationId"));
				operatorUser.setLoginName(loginName);
				operatorUser.setUserName(rs.getString("userName"));
				operatorUser.setEmail(rs.getString("email"));
				operatorUser.setRpOrganId(rs.getLong("rPOrganizationId"));
			}
		});
		return operatorUser;
	}
}
